//
//  HLHomeDiscountView.m
//  YueYue
//
//  Created by hulu001 on 2023/8/15.
//

#import "HLHomeDiscountView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "HLMatchGoldViewController.h"
#import "HLHomeDiscountCell.h"

@interface HLHomeDiscountView ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) UIImageView *backView;
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) QMUIButton *discountButton;
@property (nonatomic, strong) QMUIButton *closeButton;
@end

@implementation HLHomeDiscountView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        
    }
    return self;
}

- (void)setupView
{
    self.discountButton = [[QMUIButton alloc] init];
    _discountButton.hidden = YES;
    [_discountButton setImage:UIImageMake(@"discount_hide") forState:UIControlStateNormal];
    [_discountButton addTarget:self action:@selector(showAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_discountButton];
    [_discountButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
    }];
    
    self.backView = [[UIImageView alloc] initWithImage:UIImageMake(@"home_discount_back")];
    [self addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,124, 50) delegate:self placeholderImage:UIImageMake(@"avatar")];
    _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    _cycleScrollView.showPageControl = NO;
    _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
    _cycleScrollView.backgroundColor = UIColorClear;
    _cycleScrollView.layer.masksToBounds = YES;
    _cycleScrollView.layer.cornerRadius = 25;
    [self addSubview:_cycleScrollView];
    [_cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(4);
        make.right.mas_equalTo(-4);
        make.top.mas_equalTo(8);
        make.bottom.mas_equalTo(-8);
    }];
    
    self.closeButton = [[QMUIButton alloc] init];
    [_closeButton setImage:UIImageMake(@"discount_close") forState:UIControlStateNormal];
    [_closeButton addTarget:self action:@selector(hideAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeButton];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
    }];
    
    // 添加左右滑动手势识别器
    UISwipeGestureRecognizer *swipeLeftGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeLeftGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:swipeLeftGestureRecognizer];

    UISwipeGestureRecognizer *swipeRightGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeRightGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:swipeRightGestureRecognizer];
}


- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)recognizer {
    
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        [self showAction];
    } else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        // 向右滑动收起悬浮按钮
        [self hideAction];
    }
}

- (void)setCycleDatas:(NSArray<HLHomeListModel *> *)cycleDatas
{
    _cycleDatas = cycleDatas;
    _cycleScrollView.imageURLStringsGroup = [cycleDatas qmui_mapWithBlock:^NSString * _Nonnull(HLHomeListModel * _Nonnull item, NSInteger index) {
        return @"";
    }];
}

- (void)hideAction
{
    [UIView animateWithDuration:0.2 animations:^{
        self.discountButton.hidden = NO;
        self.closeButton.hidden = YES;
        self.backView.alpha = 0;
        self.cycleScrollView.alpha = 0;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(116);
        }];
    }];
}

- (void)showAction
{
    // 向左滑动展开悬浮按钮
    [UIView animateWithDuration:0.2 animations:^{
        self.discountButton.hidden = YES;
        self.closeButton.hidden = NO;
        self.backView.alpha = 1;
        self.cycleScrollView.alpha = 1;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(0);
        }];
    }];
}

#pragma mark SDCycleScrollViewDelegate

- (Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view
{
    return [HLHomeDiscountCell class];
}

- (void)setupCustomCell:(HLHomeDiscountCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view
{
    cell.model = self.cycleDatas[index];
    
}

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
//    HLHomeListModel *userModel = self.cycleDatas[index];
//    HLUserInfoModel *user = [[HLUserInfoModel alloc] init];
//    user.userId = userModel.userId;
//    user.videoType = [userModel videoType];
//    [HLUIHelper callWithUser:user];
    
    HLMatchGoldViewController *matchGoldViewController = [[HLMatchGoldViewController alloc] init];
    [self.qmui_viewController.navigationController pushViewController:matchGoldViewController animated:YES];
    
}


@end
